package br.com.amadeus.android.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.com.amadeus.android.entities.SocialHistory;
import br.com.amadeus.android.util.Util;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SocialHistoryDAO {
	
	public static String[] columns = new String[] { SocialHistoryDAO.ID, SocialHistoryDAO.COURSE_NAME,
		SocialHistoryDAO.USER_NAME, SocialHistoryDAO.USER_LOGIN, SocialHistoryDAO.ACTION_VALUE, 
		SocialHistoryDAO.RESOURSE_NAME, SocialHistoryDAO.CREATION_DATE,
		SocialHistoryDAO.AMADEUS_ID};
	
	public static final String ID = "_id";
	public static final String COURSE_NAME = "course_name";
	public static final String USER_NAME = "user_name";
	public static final String USER_LOGIN = "user_login";
	public static final String ACTION_VALUE = "action_value";
	public static final String RESOURSE_NAME = "resource_name";
	public static final String CREATION_DATE = "creation_date";
	public static final String AMADEUS_ID = "amadeus_id";
	
	private SQLiteDatabase db;
	
	private static SocialHistoryDAO instance = new SocialHistoryDAO();
	
	public static SocialHistoryDAO getInstance(Context ctx){
		
		if (instance.db == null || !instance.db.isOpen()) {
			DBHelper helper = new DBHelper(ctx);
			instance.db = helper.getWritableDatabase();
		}
		return instance;
	}
	
	private SocialHistoryDAO(){
		
	}
	
	public long save(SocialHistory socialHistory){
		if (socialHistory.getId() == 0) {
			return insert(socialHistory);
		}else{
			return update(socialHistory);
		}
	}

	private long update(SocialHistory socialHistory) {
		// TODO Auto-generated method stub
		return 0;
	}

	private long insert(SocialHistory socialHistory) {
		long id_social = 0;
		
		db.beginTransaction();
		try {
			SimpleDateFormat dtFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			ContentValues values = new ContentValues();
			values.put(SocialHistoryDAO.COURSE_NAME, socialHistory.getCourseName());
			values.put(SocialHistoryDAO.USER_NAME, socialHistory.getUserName());
			values.put(SocialHistoryDAO.USER_LOGIN, socialHistory.getUserLogin());
			values.put(SocialHistoryDAO.ACTION_VALUE, socialHistory.getActionValue());
			values.put(SocialHistoryDAO.RESOURSE_NAME, socialHistory.getResourceName());
			values.put(SocialHistoryDAO.CREATION_DATE,
					dtFormat.format(socialHistory.getCreationDate()));
			values.put(SocialHistoryDAO.AMADEUS_ID, socialHistory.getAmadeusID());
			
			id_social = db.insert(DBHelper.TABLE_SOCIAL_HISTORY, null, values);

			if (id_social != -1)
				db.setTransactionSuccessful();

		} finally {
			db.endTransaction();
		}
		return id_social;
	}
	
	public List<SocialHistory> getListSocialHistory(){
		List<SocialHistory> list = new ArrayList<SocialHistory>();
		Cursor c = db.query(DBHelper.TABLE_SOCIAL_HISTORY, columns, null, null, null, null, null);
		if (c.moveToFirst()) {
			int id_Id = c.getColumnIndex(CourseDAO.ID);
			int id_course_name = c.getColumnIndex(SocialHistoryDAO.COURSE_NAME);
			int id_user_name = c.getColumnIndex(SocialHistoryDAO.USER_NAME);
			int id_user_login = c.getColumnIndex(SocialHistoryDAO.USER_LOGIN);
			int id_action_value = c.getColumnIndex(SocialHistoryDAO.ACTION_VALUE);
			int id_res_name = c.getColumnIndex(SocialHistoryDAO.RESOURSE_NAME);
			int id_creation_date = c.getColumnIndex(SocialHistoryDAO.CREATION_DATE);
			int id_amadeus_id = c.getColumnIndex(SocialHistoryDAO.AMADEUS_ID);
			
			do {
				int id = c.getInt(id_Id);
				String course_name = c.getString(id_course_name);
				String user_name = c.getString(id_user_name);
				String user_login = c.getString(id_user_login);
				String action_value = c.getString(id_action_value);
				String res_name = c.getString(id_res_name);
				Date creation_date = Util.getDate(c.getString(id_creation_date), Util.FORMATO_DATA_BD);				
				int amadeus_id = c.getInt(id_amadeus_id);
				
				SocialHistory socialHistory = new SocialHistory(id, course_name, user_name, user_login, action_value, res_name, creation_date, amadeus_id);
				list.add(socialHistory);
				
			} while (c.moveToNext());
			
		}
		c.close();
		return list;
	}
	

}
